package com.csaba.swing.gui.khb;

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import com.csaba.connector.model.Customer;
import com.csaba.connector.model.Session;
import com.csaba.swing.gui.GUIContext;
import com.csaba.swing.gui.login.AbstractLoginPage;
import com.csaba.swing.gui.login.LoginDetails;
import com.csaba.swing.gui.login.LoginWizardModel;
import com.csaba.swing.gui.util.MaxLengthDocument;
import com.csaba.swing.gui.util.RegExpDocument;
import com.csaba.swing.gui.wizard.WizardContext;
import com.csaba.swing.gui.wizard.WizardDialog;
import com.csaba.util.I18N;

@SuppressWarnings( "serial" )
public class KHBLoginPage extends AbstractLoginPage
{
	private static final String LD_PIN = "password"; //$NON-NLS-1$

	private final static int LOGINID_LENGTH = 8;
	private final static int PIN_LENGTH = 6;

	private JTextField loginIdField;
	private JPasswordField pinField;
	private JCheckBox savePin;

	private boolean ready = false;

	@Override
	public void init( final WizardContext context )
	{
		super.init(context);

		add(new JLabel(I18N.getString(this, "loginId"))); //$NON-NLS-1$

		add(loginIdField = new JTextField(new RegExpDocument("[0-9]*", LOGINID_LENGTH), "", //$NON-NLS-1$ //$NON-NLS-2$
				LOGINID_LENGTH));
		add(new JLabel(I18N.getString(this, "password"))); //$NON-NLS-1$
		add(pinField = new JPasswordField(new MaxLengthDocument(PIN_LENGTH), "", PIN_LENGTH)); //$NON-NLS-1$
		add(new JSeparator());
		add(savePin = new JCheckBox(I18N.getString(this, "KHBLoginPage.savePassword"))); //$NON-NLS-1$
		savePin.setEnabled(false);

		addChangeListener(loginIdField);
		addChangeListener(pinField);
	}

	@Override
	public boolean leave( final int action )
	{
		boolean canLeave = true;
		if ( action == ACTION_NEXT )
		{
			if ( ready )
			{
				final Customer customer = new Customer();
				customer.setLoginId(loginIdField.getText());
				customer.setPassword(new String(pinField.getPassword()));
				if ( doLogin(customer) )
				{
					//store session info in wizard session
					final Session session = GUIContext.getInstance().getSession(customer);
					context.setProperty(KHBLoginWizardPages.PROPERTY_SESSION, session);

					//can go to the next page
				}
				else
				{
					canLeave = false;
				}
			}
			else
			{
				JOptionPane.showMessageDialog(this, I18N.getString(this, "msg.fillForm"));//$NON-NLS-1$
				canLeave = false;
			}
		}

		return canLeave;
	}

	@Override
	protected void updateMessage( final Component component )
	{
		super.updateMessage(component);

		final WizardDialog dialog = context.getDialog();
		boolean ready = true;

		final String loginId = loginIdField.getText();
		final int loginLength = loginId.length();

		if ( loginLength != LOGINID_LENGTH )
		{
			dialog.setWarning(I18N.getString(this, "KHBLoginPage.msg.validLoginId")); //$NON-NLS-1$
			ready = false;
		}
		else if ( pinField.getPassword().length != PIN_LENGTH )
		{
			dialog.setWarning(I18N.getString(this, "KHBLoginPage.msg.validPassword")); //$NON-NLS-1$
			ready = false;
		}

		savePin.setEnabled(( (LoginWizardModel) context.getWizardModel() ).isSaveDetails());
		if ( !savePin.isEnabled() )
			savePin.setSelected(false);

		if ( ready )
		{
			dialog.clearMessage();
		}
		this.ready = ready;
	}

	@Override
	protected LoginDetails getDetailsToSave( final Customer customer )
	{
		final LoginDetails details = new LoginDetails(customer.getBank().getName(), customer.getLoginId());

		if ( savePin.isSelected() )
			details.put(LD_PIN, customer.getPassword());

		return details;
	}

	@Override
	protected void setSavedDetails( final LoginDetails details )
	{
		loginIdField.setText(details.getLoginId());
		final String pin = details.get(LD_PIN);
		pinField.setText(pin);
		savePin.setSelected(pin != null);
	}

}
